package com.suredy.app.porject.ctrl.flow;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.constants.FlowType;
import com.suredy.app.porject.service.FlowOfBid2UserSrv;
import com.suredy.app.porject.service.FlowOfContract2UserSrv;
import com.suredy.core.mvc.model.MessageModel;
import com.suredy.security.entity.UserEntity;

/**
 * 流程控制器
 * 
 * @author VIVID.G
 * @since 2015-12-25
 * @version v0.1
 */
@Controller
@RequestMapping("/flow")
public class FlowCtrlForProject extends FlowCtrl {

	@Autowired
	private FlowOfBid2UserSrv flowOfBid2UserSrv;

	@Autowired
	private FlowOfContract2UserSrv flowOfContract2UserSrv;

	/**
	 * 选人发送
	 * 
	 * @param flowType 流程类型
	 * @param roleCode 用户角色
	 * @param id 业务数据的id
	 * @param nextStatus 下一个状态
	 * @param single 是否单选
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/send-with-user/{flowType}/{roleCode}/{id}/{nextStatus}/{single}", method = RequestMethod.GET)
	public String sendWithSelectUserView(@PathVariable FlowType flowType, @PathVariable String roleCode, @PathVariable String id, @PathVariable FlowStatus nextStatus, @PathVariable boolean single, Model model) {
		String view = "app/flow/send-with-user";

		if (flowType == null || StringUtils.isBlank(roleCode) || StringUtils.isBlank(id) || nextStatus == null) {
			model.addAttribute("invalid", true);
			return view;
		}

		List<UserEntity> users = this.getUserByRole(roleCode);

		if (users == null || users.isEmpty()) {
			model.addAttribute("no_user", true);
			return view;
		}

		model.addAttribute("users", users);
		model.addAttribute("type", flowType);
		model.addAttribute("id", id);
		model.addAttribute("status", nextStatus);
		model.addAttribute("single", single);

		return view;
	}

	/**
	 * @param type 流程类型
	 * @param id 业务数据id
	 * @param userIds 用户id
	 * @param status 状态
	 * @return
	 */
	@RequestMapping(value = "/send-with-user", method = RequestMethod.POST)
	@ResponseBody
	public Object sendWithUser(FlowType type, String id, String[] userIds, FlowStatus status) {
		if (type == null || StringUtils.isBlank(id) || userIds == null || userIds.length <= 0 || status == null)
			return new MessageModel(false, "操作失败，提交的信息无效！", null);

		Object result = null;

		switch (type) {
			case flow_bid:
				result = this.sendWithUserForBid(id, userIds, status);
				break;
			case flow_contract:
				result = this.sendWithUserForContract(id, userIds, status);
				break;
		}

		return result == null ? new MessageModel(false, "操作失败，未找到对应的发送配置信息！", null) : result;
	}

	/**
	 * @param id 业务id
	 * @param flowType 流程类型
	 * @param status 流程状态
	 * @return
	 */
	@RequestMapping(value = "/change-status", method = RequestMethod.POST)
	@ResponseBody
	public Object changeStatus(String id, FlowType type, FlowStatus status) {
		if (type == null || StringUtils.isBlank(id) || status == null)
			return new MessageModel(false, "操作失败，提交的信息无效！", null);

		Object result = null;

		switch (type) {
			case flow_bid:
				result = this.changeStatusForBid(id, status);
				break;
			case flow_contract:
				result = this.changeStatusForContract(id, status);
				break;
		}

		return result == null ? new MessageModel(false, "操作失败，未找到对应的发送配置信息！", null) : result;
	}

	public final MessageModel sendWithUserForBid(String bidId, String[] userIds, FlowStatus status) {
		if (StringUtils.isBlank(bidId))
			return new MessageModel(false, "操作失败，无效的投标信息！", null);

		if (status == null)
			return new MessageModel(false, "操作失败，无效的流程状态信息！", null);

		if (userIds == null || userIds.length <= 0)
			return new MessageModel(false, "操作失败，无效的用户信息！", null);

		boolean success = this.flowOfBid2UserSrv.sendWithUsers(bidId, userIds, status);

		if (!success)
			return new MessageModel(false, "操作失败，无法进行发送操作！", null);

		return new MessageModel(true, "操作成功！", null);
	}

	public final MessageModel sendWithUserForContract(String contractId, String[] userIds, FlowStatus status) {
		if (StringUtils.isBlank(contractId))
			return new MessageModel(false, "操作失败，无效的合同信息！", null);

		if (status == null)
			return new MessageModel(false, "操作失败，无效的流程状态信息！", null);

		if (userIds == null || userIds.length <= 0)
			return new MessageModel(false, "操作失败，无效的用户信息！", null);

		boolean success = this.flowOfContract2UserSrv.sendWithUsers(contractId, userIds, status);

		if (!success)
			return new MessageModel(false, "操作失败，无法进行发送操作！", null);

		return new MessageModel(true, "操作成功！", null);
	}

	public final MessageModel changeStatusForBid(String bidId, FlowStatus status) {
		if (StringUtils.isBlank(bidId))
			return new MessageModel(false, "操作失败，无效的投标信息！", null);

		if (status == null)
			return new MessageModel(false, "操作失败，无效的流程状态信息！", null);

		if (!this.flowOfBid2UserSrv.changeStatus(bidId, status))
			return new MessageModel(false, "操作失败，无法更新状态信息！", null);

		return new MessageModel(true, "操作成功！", null);
	}

	public final MessageModel changeStatusForContract(String contractId, FlowStatus status) {
		if (StringUtils.isBlank(contractId))
			return new MessageModel(false, "操作失败，无效的合同信息！", null);

		if (status == null)
			return new MessageModel(false, "操作失败，无效的流程状态信息！", null);

		if (!this.flowOfContract2UserSrv.changeStatus(contractId, status))
			return new MessageModel(false, "操作失败，无法更新状态信息！", null);

		return new MessageModel(true, "操作成功！", null);
	}

}
